#region [ using ]
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
#endregion

/// <summary>
/// Summary description for Menager
/// </summary>
public static class Menager
{
	#region [ Load ]
	/// <summary>
	/// Creates HashTabel from DataTable.
	/// </summary>
	/// <returns>HashTabel</returns>
	private static Hashtable Load()
	{
		DataTable dt = null;
		string filePath = GetFileName();
		try
		{
			// reciving DataTable
			dt = FileLoader.Fill(filePath);
		}
		catch (Exception)
		{
			return null;
		}
		Hashtable ht = new Hashtable();
		// creating HashTable
		if (dt != null)
		{
			for (int i = 0; i < dt.Rows.Count; i++)
			{
				ht.Add(dt.Rows[i]["id"].ToString(), dt.Rows[i]["message"].ToString());
			}
		}
		return ht;
	}
	#endregion

	#region [ GetFileName ]
	/// <summary>
	/// Gets the name and the path of the XML file with error list.
	/// </summary>
	/// <returns>Path to the XML file</returns>
	private static string GetFileName()
	{
		string filePath;
		//getting file name from WebCofig file
		filePath = System.Web.HttpContext.Current.Server.MapPath("~") + @"\App_Data\" + ConfigurationManager.AppSettings["errorListFileName"];
		return filePath;
	}
	#endregion

	#region [ GetErrorText ]
	/// <summary>
	/// Getting text of error by its code from the hash table.
	/// </summary>
	/// <param name="key">code of the error message.</param>
	/// <returns></returns>
	public static string GetErrorText(string key)
	{
		// Getting HashTable from GetHastTable method
		Hashtable ht = GetHastTable();
		if (ht != null)
		{
			if (ht.Contains(key))
				return (string)ht[key];
		}
		// if no such error exists returned this message
		return key;
	}
	#endregion

	#region [ GetHastTable ]
	/// <summary>
	/// Returns HashTable.
	/// </summary>
	/// <returns></returns>
	private static Hashtable GetHastTable()
	{
		// if such HashTable already exist - we're returning it
		object h = System.Web.HttpContext.Current.Cache["ErrorsCoolection"];
		if (h != null && h is Hashtable)
			return (Hashtable)h;
		// if not - we're creating a new one with 1 hour of life time
		Hashtable ht = Load();
		if (ht != null)
			System.Web.HttpContext.Current.Cache.Insert("ErrorsCoolection", ht, new System.Web.Caching.CacheDependency(GetFileName()), DateTime.Now.AddHours(1), System.Web.Caching.Cache.NoSlidingExpiration);
		//==================================================================
		return ht;
	}
	#endregion
}